package com.travel.controller;

import com.travel.entity.Travel;
import com.travel.req.TravelAddReq;
import com.travel.service.TravelService;
import com.travel.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
public class TravelController {

    @Autowired
    TravelService travelService;

    @GetMapping("/all")
    public ResponseEntity getAll(){
        ResponseEntity resp = new ResponseEntity();
        resp.setData(travelService.getAll());
        return resp;
    }

    @GetMapping("/one/{id}")
    public ResponseEntity getOne(@PathVariable("id") Integer id){
        ResponseEntity resp = new ResponseEntity();
        resp.setData(travelService.getOne(id));
        return resp;
    }

    @PostMapping("/add")
    public ResponseEntity add(@Valid TravelAddReq travelAddReq){
        ResponseEntity resp = new ResponseEntity();

        //拷贝对象
        Travel travel = new Travel();
        travel.setArea(travelAddReq.getArea());
        travel.setTime(travelAddReq.getTime());
        travel.setFelling(travelAddReq.getFelling());
        travelService.add(travel);
        resp.setData(travel.getId());
        return resp;
    }

    @GetMapping("/delete/{id}")
    public ResponseEntity delete(@PathVariable("id") Integer id) {
        travelService.delete(id);
        ResponseEntity resp = new ResponseEntity();
        return resp;
    }
}
